﻿using System;
using UI.Dialogs.Dictionaries.Builders;

namespace UI.Dialogs.Dictionaries
{
    /// <summary>
    /// Manager class for dictionary building
    /// </summary>
    public class DictionaryManager
    {
        DictionaryFormBase _form;
        DictionaryBuilder _builder;

        /// <summary>
        /// Initializes a new instance of the <see cref="DictionaryManager"/> class.
        /// </summary>
        /// <param name="type">The type of the dictionary.</param>
        public DictionaryManager(Dictionary type)
        {
            _form = new DictionaryFormBase();

            switch (type)
            {
                case Dictionary.Publisher:
                    _builder = new PublisherBuilder(_form);
                    break;
                case Dictionary.Binding:
                    _builder = new BindingBuilder(_form);
                    break;
                case Dictionary.Condition:
                    _builder = new ConditionBuilder(_form);
                    break;
                default:
                    throw new ArgumentOutOfRangeException("type");
            }

            _builder.Build();
            _form.AddClicked+= delegate
                                    {
                                       _builder.UpdateGrid(UpdateOperation.Insert, null);
                                    };
            _form.SaveClicked += delegate
                                     {
                                         _builder.UpdateGrid(UpdateOperation.Update, null);
                                     };
        }

        /// <summary>
        /// Shows this instance.
        /// </summary>
        public void Show()
        {
            _form.Show();
        }

        /// <summary>
        /// Sets the add default.
        /// </summary>
        public void SetAddDefault()
        {
            _form.SetAddDefault();
        }

        /// <summary>
        /// Sets the edit default.
        /// </summary>
        public void SetEditDefault()
        {
            _form.SetEditDefault();
        }

        /// <summary>
        /// Sets the edit default.
        /// </summary>
        /// <param name="item">The item.</param>
        public void SetEditDefault(object item)
        {
            SetEditDefault();
            _builder.SetSelected(item);
        }
    }
}